import random as r
import sympy as sp

# 定义自变量
x_sym,y_sym = sp.symbols('x y')

# 函数
f = x_sym ** 2 + y_sym ** 2 - 2 * x_sym * y_sym

# 求导数
deritative_expr = sp.diff(f,x_sym)
deritative1_expr = sp.diff(f,y_sym)

#生成变量值
x = r.randint(-10,10)
y = r.randint(-10,10)

# 超参数设置
epchos = 1000000
learning_rate = 0.001

# 将符号表达式替换为具体的数值，以计算在当前x和y下的导数值  
# deritative = deritative_expr.subs({x_sym: x, y_sym: y})  
# deritative1 = deritative1_expr.subs({x_sym: x, y_sym: y}) 

# 模型训练
for i in range(epchos):
    x = x - (2 * x - 2 * y) * learning_rate
    y = y - (2 * y - 2 * x) * learning_rate
    # x = x - deritative * learning_rate
    # y = y - deritative1 * learning_rate
    # deritative = deritative_expr.subs({x_sym: x, y_sym: y})  
    # deritative1 = deritative1_expr.subs({x_sym: x, y_sym: y}) 
    z = x ** 2 + y ** 2 - 2 * x * y
    if z == 0:
        print(x,y,z)